//
//  SWSearchNaviView.m
//  sc-cyan-ios
//
//  Created by lvjian01 on 2017/12/20.
//  Copyright © 2017年 greenbuilding. All rights reserved.
//

#import "SWSearchNaviView.h"

@interface SWSearchNaviView ()

@property (nonatomic, strong) UIBarButtonItem *backItem;//(不再使用)
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) UITextField *searchField;
@property (nonatomic, strong) UIButton *backButton;

@end

@implementation SWSearchNaviView

static CGFloat searchButtonWidth = 54;
static CGFloat searchButtonHeight = 30;
static CGFloat backButtonWidth = 30;

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    
    return self;
}

- (UIButton *)backButton{
    if (!_backButton){
        _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, backButtonWidth, kTopBarHeight)];
        [_backButton addTarget:self action:@selector(popViewControllerAnimated) forControlEvents:UIControlEventTouchUpInside];
        [_backButton setImage:[UIImage imageNamed:@"tongyong_fanhui"] forState:UIControlStateNormal];
    }
    return _backButton;
}

- (UIBarButtonItem *)backItem{
    if (!_backItem) {
        _backItem = [UIBarButtonItem itemWithIcon:@"tongyong_fanhui" highIcon:@"" target:self action:@selector(popViewControllerAnimated)];
    }
    return _backItem;
}

- (UIButton *)searchButton{
    if (!_searchButton) {
//        CGFloat searchButtonWidth = 54;
//        CGFloat searchButtonHeight = 30;
        _searchButton = [[UIButton alloc] initWithFrame:CGRectMake(Main_Screen_Width - searchButtonWidth - 10, (kTopBarHeight - searchButtonHeight) / 2, searchButtonWidth, searchButtonHeight)];
        _searchButton.backgroundColor = [UIColor mainBodyColor];
        _searchButton.cornerRadius = 2;
        [_searchButton setTitle:@"搜索" forState:UIControlStateNormal];
        [_searchButton addTarget:self action:@selector(searchTextField) forControlEvents:UIControlEventTouchUpInside];
        [_searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _searchButton.titleLabel.font = SYSTEMFONT(13);
    }
    return _searchButton;
}

- (UITextField *)searchField{
    if (!_searchField) {
//        _searchField = [[UITextField alloc] initWithFrame:CGRectMake(_backItem.customView.width, _searchButton.y, _searchButton.x - _backItem.customView.width - 5, searchButtonHeight)];
        _searchField = [[UITextField alloc] initWithFrame:CGRectMake(backButtonWidth, _searchButton.y, _searchButton.x - backButtonWidth - 5, searchButtonHeight)];

        _searchField.borderStyle = UITextBorderStyleRoundedRect;
        _searchField.borderWidth = 1;
        _searchField.cornerRadius = 2;
        _searchField.borderColor = [UIColor gb_line_d4d4d4];
        _searchField.backgroundColor = [UIColor gb_background_f6f6f6];
        _searchField.placeholder = @"请输入关键词";
        _searchField.font = [UIFont systemFontOfSize:13.0f];
    }
    return _searchField;
}

- (void)initUI{
    
    self.backgroundColor = [UIColor gb_naviBarBackgroundcolor];
   
//    [self addSubview:self.backItem.customView];
    [self addSubview:self.backButton];
    
    [self addSubview:self.searchButton];
    
    [self addSubview:self.searchField];
    
}

- (void)popViewControllerAnimated{
    
    if (self.popViewBack) {
        self.popViewBack();

    }
    
}

- (void)searchTextField{
//    在这里做判断field是否为空
    NSString *searchText = self.searchField.text;
    if (!searchText || searchText.length == 0) {
        NSLog(@"请输入关键字");
        return;
    }
    
    if (self.searchText) {
        self.searchText(self.searchField.text);
    }
}


@end
